#include "cas_keep.h"
#include "scheduler.h"
#include "slave.h"
#include "task.h"
#include "MemPool.h"
int ref_count(struct task *self) { return self->ref_count_t; }

/*int is_stolen_task(struct task* self)
{
    return (self->extra_state) & 0x80;
}*/
void slave_task_constructor(struct task *self)
{
    // struct task self ;
//printf("slave_task_cons\n");    
self->ref_count_t = -1;
#ifdef REUSE_GRAPH
    self->backup_ref = -1;
    self->reused = 0;
    self->stored = 0;
    self->is_base_task = 1;
#endif
    self->affinity_id = INIT_AFFINITY;
#if defined(USE_PRIORITY_QUEUE) || defined(LOCAL_MULTI_PRIORI)
    self->priority_id = 0;
#endif
    // self->spawn_order_id = 0;
    self->next = NULL;
    // self->extra_state = 0;
    // return self;
}
#ifdef CONCURRENT_CONSTRUCT_GRAPH
/*
inline void set_pre(struct task* self,int p)
{
    self->pre = p;
}
inline int pre_count(struct task* self)
{
    return self->pre;
}
*/
#endif

void spawn(struct task *t) { slave_spawn_to_id(t); }

void setparent(struct task *child_task, struct task *t)
{
    child_task->parent = t;
    if (t != NULL)
    {
         updt_subw(1, &(t->_countdownToBeWokenUp)); 
         //(t->_countdownToBeWokenUp)--;
    }
    //printf("%p父任务的孩子个数%d\n",t,t->_countdownToBeWokenUp);

}
struct task *getparent(struct task *t) { return t->parent; }
void initprecount(struct task *t)
{
    t->_predecessorCount = -1;
    t->_countdownToBeWokenUp = -1;
    t->_endExecute = 0;
    // t->countlock=0;
}

void initialize(struct DataAccess *cur, int type, struct task *originator, int _accessFlags_, int top_) {
    cur->_originator=originator;
    cur->_successor=NULL;
    // cur->_child=NULL;
    cur->_type=type;
    cur->top=top_;
    cur->_accessFlags=_accessFlags_;
    cur->deleted=0;
   
} 

struct new_slave_table *init_new_slave_table(int id) {  //初始化
  //struct new_slave_table tables[64];
  struct new_slave_table *slave_table;
    int i;
    for (i = 0; i < 1; i++) {
        slave_table->items[i].data_addr=NULL;
        slave_table->items[i].data_access=NULL;
        slave_table->items[i].succ=NULL;
        // tables.items[i].data_addr = NULL;
        // tables.items[i].data_access= NULL;
        // tables.items[i].succ = NULL;
    }
    //tables.col_idx = 0;
     slave_table->col_idx=0;
     int j;
    for (j = 0; j < 1; j++) {
        slave_table->col_items[j].data_addr=NULL;
        slave_table->col_items[j].data_access=NULL;
        slave_table->col_items[j].succ=NULL;
        // t->_dataAccesses._subaccessBottomMap.col_items[i].succ=NULL;
        // tables.col_items[i].data_addr = NULL;
        // tables.col_items[i].data_access = NULL;
        // tables.col_items[i].succ = NULL;
    }
    return slave_table;
}
void initTaskDataAccess(struct task *t, int _size)
{
    t->_dataAccesses._accessArray =slave_acemesh_myalloc_aligned_16(sizeof(struct DataAccess)*_size,_MYID); 
    t->_dataAccesses._addressArray =slave_acemesh_myalloc_aligned_16(sizeof(void*)*_size,_MYID); 
    t->_dataAccesses._currentIndex = 0;
    t->_dataAccesses._subaccessBottomMap = NULL;
}
struct TaskDataAccesses *getDataAccesses(struct task *t) { return &(t->_dataAccesses); }
struct DataAccess *findAccess(struct task *t, void *address)
{
    // if (_accessMap != nullptr) {
    // 	access_map_t::iterator itAccess = _accessMap->find(address);
    // 	if (itAccess != _accessMap->end())
    // 		return &itAccess->second;
    // } else {
    int size = t->_dataAccesses._currentIndex;
    for (size_t i = 0; i < size; ++i) {
        if (t->_dataAccesses._addressArray[i] == address)
            return &(t->_dataAccesses._accessArray[i]);
    }
    //}

    return NULL;
}
// void setChild(struct DataAccess *parent, struct DataAccess *child)
// {
//     // (parent->_child).store(child, std::memory_order_seq_cst);
 
//     parent->_child = child;
//      updt_subw(1, &((child->_originator)->_predecessorCount));
//    // ((child->_originator)->_predecessorCount)--;
// }
void setSuccessor(struct DataAccess *pre, struct DataAccess *successor)
{
    pre->_successor = successor;
   // printf("前驱任务%p,后继任务%p\n",pre->_originator,successor->_originator);
   // int val = 1;
    updt_subw(1, &((successor->_originator)->_predecessorCount));  //!!!!多个helper操作时，需要确保对任务前驱计数操作的原子性
   //((successor->_originator)->_predecessorCount)--;
   asm volatile ("memb\n");
}
void set_unblock_Successor(struct DataAccess *pre, struct DataAccess *successor)
{
    pre->_successor = successor;
}
int predec_count(struct task *t) { return t->_predecessorCount; }
int dec_predecessorCount(struct task *t)
{
    int res = (t->_predecessorCount -= 1);
    // assert(res >= 0);
    return (res == 0);
}
int child_count(struct task *t) { return t->_countdownToBeWokenUp; }
int dec_childCount(struct task *t)
{
    int res = (t->_countdownToBeWokenUp -= 1);
    // assert(res >= 0);
    return (res == 0);
}
int inc_predecessorCount(struct task *t) { t->_predecessorCount += 1; }
